package com.sunny.hp.common.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.FileCopyUtils;
import org.springframework.web.servlet.view.AbstractView;

/**
 * ******************************************************************************
 * <PRE> * DownloadView *
 * 1. Project	: ncomz-erp
 * 2. Package	: com.ncomz.nzeip.common.servlet
 * 3. FileName	: DownloadView.java
 * 4. 작성자		: P045701
 * 5. 작성일		: 2014. 6. 2. 오후 5:06:52
 * 6. 설명		:
 * </PRE>
 *******************************************************************************
 */
public class DownloadView extends AbstractView {
	
	public DownloadView(){
		this.setContentType("application/octet-stream");
	}

	/**
	 * <PRE>
	 * 1. MethodName	: renderMergedOutputModel
	 * 2. ClassName		: DownloadView
	 * 3. Commnet		: 
	 * 4. 작성자			: P044826
	 * 5. 작성일			: 2014. 1. 16. 오후 3:58:34
	 * </PRE>
	 * 		@param arg0
	 * 		@param arg1
	 * 		@param arg2
	 * 		@throws Exception
	 */
	@Override
	protected void renderMergedOutputModel(Map<String, Object> model,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		File file = (File)model.get("downloadFile");
		response.setContentType(this.getContentType());
		response.setContentLength((int)file.length());
		response.setHeader("Content-Disposition", "attachment;fileName="+model.get("downloadFileName")+";");
		response.setHeader("Content-Transfer-Encoding", "binary");
		OutputStream out = response.getOutputStream();
		FileInputStream fis = null;
		try{
			fis = new FileInputStream(file);
			FileCopyUtils.copy(fis, out);
		}catch(Exception e){
			
		}finally{
			if(fis !=null){
				try{
					fis.close();
				}
				catch(Exception ex){
					
				}
			}
		}
		out.flush();
	}

}
